package com.iterator.service.web.userManager;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.iterator.bean.web.lyh.bo.*;
import com.iterator.bean.web.lyh.utils.DataTotalItemsVO;
import com.iterator.bean.web.lyh.vo.AddressVO;
import com.iterator.bean.web.pojo.lyh.*;
import com.iterator.mapper.web.userManager.*;
import com.iterator.util.StringTrim;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    MallUserMapper userMapper;
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    MallCollectMapper collectMapper;
    @Autowired
    MallFootprintMapper footprintMapper;
    @Autowired
    MallSearchHistoryMapper historyMapper;
    @Autowired
    MallFeedbackMapper feedbackMapper;

    @Override
    public DataTotalItemsVO userList(UserListBO listBO) {
        MallUserExample userExample = new MallUserExample();
        //分页
        PageHelper.startPage(listBO.getPage(), listBO.getLimit());

        //去除空格
        listBO.setUsername(StringTrim.trim(listBO.getUsername()));
        listBO.setMobile(StringTrim.trim(listBO.getMobile()));

        //设置排序
        userExample.setOrderByClause(listBO.getSort() + " " + listBO.getOrder());
        //条件查询
        MallUserExample.Criteria criteria = userExample.createCriteria();
        if (listBO.getUsername() != null && listBO.getUsername().length() != 0) {
            criteria.andUsernameLike("%" + listBO.getUsername() + "%");
        }
        if (listBO.getMobile() != null && listBO.getMobile().length() != 0) {
            criteria.andMobileLike("%" + listBO.getMobile() + "%");
        }

        List<User> users = userMapper.selectByExample(userExample);

        //分页前的原数据量
        PageInfo<User> pageInfo = new PageInfo<>(users);
        long total = pageInfo.getTotal();

        DataTotalItemsVO dataVO = new DataTotalItemsVO();
        dataVO.setTotal(total);
        dataVO.setItems(users);
        return dataVO;
    }

    @Override
    public DataTotalItemsVO addressList(AddressListBO listBO) {
        //分页
        PageHelper.startPage(listBO.getPage(), listBO.getLimit());

        //去除空格
        listBO.setName(StringTrim.trim(listBO.getName()));

        if (listBO.getName() != null && listBO.getName().length() == 0) {
            listBO.setName(null);
        }
        if (listBO.getName() != null && listBO.getName().length() != 0) {
            listBO.setName("%" + listBO.getName() + "%");
        }
        List<AddressVO> addressList = addressMapper.addressList(listBO);

        //分页前的原数据量
        PageInfo<AddressVO> pageInfo = new PageInfo<>(addressList);
        long total = pageInfo.getTotal();

        DataTotalItemsVO dataVO = new DataTotalItemsVO();
        dataVO.setTotal(total);
        dataVO.setItems(addressList);
        return dataVO;
    }

    @Override
    public DataTotalItemsVO collectList(CollectListBO listBO) {
        MallCollectExample collectExample = new MallCollectExample();
        //分页
        PageHelper.startPage(listBO.getPage(), listBO.getLimit());
        //设置排序
        collectExample.setOrderByClause(listBO.getSort() + " " + listBO.getOrder());
        //条件查询
        MallCollectExample.Criteria criteria = collectExample.createCriteria();
        if (listBO.getUserId() != null) {
            criteria.andUserIdEqualTo(listBO.getUserId());
        }
        if (listBO.getValueId() != null) {
            criteria.andValueIdEqualTo(listBO.getValueId());
        }
        List<MallCollect> mallCollects = collectMapper.selectByExample(collectExample);

        //分页前的原数据量
        PageInfo<MallCollect> pageInfo = new PageInfo<>(mallCollects);
        long total = pageInfo.getTotal();

        DataTotalItemsVO dataVO = new DataTotalItemsVO();
        dataVO.setTotal(total);
        dataVO.setItems(mallCollects);
        return dataVO;
    }

    @Override
    public DataTotalItemsVO footprintList(FootprintListBO listBO) {
        MallFootprintExample footprintExample = new MallFootprintExample();
        //分页
        PageHelper.startPage(listBO.getPage(), listBO.getLimit());
        //设置排序
        footprintExample.setOrderByClause(listBO.getSort() + " " + listBO.getOrder());
        //条件查询
        MallFootprintExample.Criteria criteria = footprintExample.createCriteria();
        if (listBO.getUserId() != null) {
            criteria.andUserIdEqualTo(listBO.getUserId());
        }
        if (listBO.getGoodsId() != null) {
            criteria.andGoodsIdEqualTo(listBO.getGoodsId());
        }
        List<MallFootprint> mallFootprints = footprintMapper.selectByExample(footprintExample);

        //分页前的原数据量
        PageInfo<MallFootprint> pageInfo = new PageInfo<>(mallFootprints);
        long total = pageInfo.getTotal();

        DataTotalItemsVO dataVO = new DataTotalItemsVO();
        dataVO.setTotal(total);
        dataVO.setItems(mallFootprints);
        return dataVO;
    }

    @Override
    public DataTotalItemsVO historyList(HistoryListBO listBO) {
        MallSearchHistoryExample historyExample = new MallSearchHistoryExample();
        //分页
        PageHelper.startPage(listBO.getPage(), listBO.getLimit());

        //去除空格
        listBO.setKeyword(StringTrim.trim(listBO.getKeyword()));
        //设置排序
        historyExample.setOrderByClause(listBO.getSort() + " " + listBO.getOrder());
        //条件查询
        MallSearchHistoryExample.Criteria criteria = historyExample.createCriteria();
        if (listBO.getUserId() != null) {
            criteria.andUserIdEqualTo(listBO.getUserId());
        }
        if (listBO.getKeyword() != null && listBO.getKeyword().length() != 0) {
            criteria.andKeywordLike("%" + listBO.getKeyword() + "%");
        }
        List<MallSearchHistory> mallSearchHistories = historyMapper.selectByExample(historyExample);

        //分页前的原数据量
        PageInfo<MallSearchHistory> pageInfo = new PageInfo<>(mallSearchHistories);
        long total = pageInfo.getTotal();

        DataTotalItemsVO dataVO = new DataTotalItemsVO();
        dataVO.setTotal(total);
        dataVO.setItems(mallSearchHistories);
        return dataVO;
    }

    @Override
    public DataTotalItemsVO feedbackList(FeedbackListBO listBO) {
        MallFeedbackExample feedbackExample = new MallFeedbackExample();
        //分页
        PageHelper.startPage(listBO.getPage(), listBO.getLimit());
        //去除空格
        listBO.setUsername(StringTrim.trim(listBO.getUsername()));
        //设置排序
        feedbackExample.setOrderByClause(listBO.getSort() + " " + listBO.getOrder());
        //条件查询
        MallFeedbackExample.Criteria criteria = feedbackExample.createCriteria();
        if (listBO.getUsername() != null&&listBO.getUsername().length()!=0) {
            criteria.andUsernameLike("%" + listBO.getUsername() + "%");
        }
        if (listBO.getId() != null) {
            criteria.andIdEqualTo(listBO.getId());
        }
        List<MallFeedback> mallFeedbacks = feedbackMapper.selectByExample(feedbackExample);

        //分页前的原数据量
        PageInfo<MallFeedback> pageInfo = new PageInfo<>(mallFeedbacks);
        long total = pageInfo.getTotal();

        DataTotalItemsVO dataVO = new DataTotalItemsVO();
        dataVO.setTotal(total);
        dataVO.setItems(mallFeedbacks);
        return dataVO;
    }
}
